package com.team9.fitness.service;

import com.team9.fitness.entity.FoodInfo;
import com.team9.fitness.mapper.FoodInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 食物信息服务
 */
@Service
@Transactional
@Slf4j
public class FoodInfoService {

    @Autowired
    private FoodInfoMapper foodInfoMapper;

    /**
     * 创建食物信息
     */
    public FoodInfo createFoodInfo(FoodInfo foodInfo) {
        foodInfo.setCreatedAtIfNull();
        foodInfo.setUpdatedAtIfNull();
        foodInfoMapper.insert(foodInfo);
        return foodInfo;
    }

    /**
     * 根据ID获取食物信息
     */
    public FoodInfo getFoodInfoById(Long id) {
        return foodInfoMapper.selectById(id);
    }

    /**
     * 获取所有食物信息
     */
    public List<FoodInfo> getAllFoodInfo() {
        return foodInfoMapper.selectAll();
    }

    /**
     * 根据分类获取食物信息
     */
    public List<FoodInfo> getFoodInfoByCategory(String category) {
        return foodInfoMapper.selectByCategory(category);
    }

    /**
     * 根据名称搜索食物信息
     */
    public List<FoodInfo> searchFoodInfoByName(String keyword) {
        return foodInfoMapper.searchByName(keyword);
    }

    /**
     * 获取所有食物分类
     */
    public List<String> getAllCategories() {
        return foodInfoMapper.selectAllCategories();
    }

    /**
     * 更新食物信息
     */
    public FoodInfo updateFoodInfo(FoodInfo foodInfo) {
        foodInfo.setUpdatedAtIfNull();
        foodInfoMapper.update(foodInfo);
        return foodInfo;
    }

    /**
     * 删除食物信息
     */
    public void deleteFoodInfo(Long id) {
        foodInfoMapper.deleteById(id);
    }
}

